library(tidyverse)
## Note: please create a folder "SP_journal_plot" in Downloads
## Put all the files you download in this folder
## This process is essential to make the codes work!
base_path <- file.path("~/Downloads", "SP_journal_plot")
db_file_path <- function(file_name) {
  file.path(base_path, file_name)
}
db_file <- rio::import(db_file_path("db_world_for_plot.xlsx"))

abs_plot_path <- function(file_name) {
  file.path(base_path, "plots", file_name)
}

gen_pair <- function(raw_file,
                     unique_data = TRUE,
                     swap = TRUE) {
  if (unique_data) {
    swap <- TRUE
  }

  # Create two new ccode variables to ensure country1_code is the smaller one and country2_code is the larger one
  prepared <- raw_file %>%
    select(
      country1_cown,
      country2_cown,
      year,
      month,
      country1_iso3c,
      country2_iso3c
    ) %>%
    mutate(
      first_is_smaller = replace_na(country1_cown < country2_cown, TRUE),
      swap_country1_iso_code = ifelse(first_is_smaller, country1_iso3c, country2_iso3c),
      swap_country2_iso_code = ifelse(first_is_smaller, country2_iso3c, country1_iso3c),
      swap_country1_cow_num = ifelse(first_is_smaller, country1_cown, country2_cown),
      swap_country2_cow_num = ifelse(first_is_smaller, country2_cown, country1_cown),
      pair_iso_code = paste(swap_country1_iso_code, swap_country2_iso_code, sep = "_"),
      pair_cow_num = paste(swap_country1_cow_num, swap_country2_cow_num, sep = "_"),
    ) %>%
    rename(
      country1_iso_code = if_else(swap, "swap_country1_iso_code", "country1_iso3c"),
      country2_iso_code = if_else(swap, "swap_country2_iso_code", "country2_iso3c"),
      country1_cow_num = if_else(swap, "swap_country1_cow_num", "country1_cown"),
      country2_cow_num = if_else(swap, "swap_country2_cow_num", "country2_cown")
    ) %>%
    select(
      country1_iso_code,
      country1_cow_num,
      country2_iso_code,
      country2_cow_num,
      pair_iso_code,
      pair_cow_num,
      year,
      month
    )

  if (unique_data) {
    return(unique(prepared))
  }
  prepared
}
extract_partners <- function(country_name, dbfile = db_file) {
  country_code <- countrycode::countryname(country_name, "cown")
  country_iso3c <- countrycode::countryname(country_name, "iso3c")
  sp <- db_file %>%
    filter(country1_cown == country_code |
      country2_cown == country_code) %>%
    select(country1_iso3c, country2_iso3c) %>%
    mutate(
      country2_iso3c = if_else(
        country2_iso3c == country_iso3c,
        country1_iso3c,
        country2_iso3c
      ),
      country1_iso3c = if_else(
        country1_iso3c != country_iso3c,
        country_iso3c,
        country1_iso3c
      )
    ) %>%
    unique() %>%
    select(country2_iso3c) %>%
    rename(SP_country = country2_iso3c) %>%
    mutate(
      full_name = countrycode::countrycode(SP_country, "iso3c", "country.name.en"),
      cown = countrycode::countrycode(SP_country, "iso3c", "cown")
    )
  sp
}
extract_certain_countries <- function(iso3c, dbfile = db_file) {
  dbfile %>%
    filter(country1_iso3c %in% iso3c | country2_iso3c %in% iso3c)
}

# theme setting -----------------------------------------------------------
ggplot2::theme_set(
  ggthemes::theme_clean() +
    theme(
      axis.title.x = element_blank(),
      axis.text.x = element_text(size = 10),
      axis.title.y.left = element_text(size = 12),
      axis.text.y.left = element_text(size = 10),
      axis.title.y.right = element_text(size = 12),
      axis.text.y.right = element_text(size = 10),
      legend.position = "bottom",
      legend.text = element_text(size = 12),
      legend.spacing.x = unit(.2, "in"),
      legend.key.width = unit(.5, "in")
    )
)
save_plot <-
  function(file_name,
           save_plot = last_plot(),
           width = 12,
           height = 9) {
    date_prefix <- format(Sys.Date(), "%Y%m%d")
    dated_file_name <- paste0(date_prefix, "_", file_name)
    file_path <- file.path(base_path, dated_file_name)
    ggsave(
      file_path,
      plot = save_plot,
      width = width,
      height = height,
      units = "in"
    )
  }
